home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 4 / Meeting Pearls Vol. IV (1996)(GTI - Schatztruhe)[!].iso / Pearls / arc / UU-Coder / BCode / bcode.doc next >
Text File  |  1996-08-10  |  19KB  |  349 lines

  1. X-SystemInfo: SWIPnet: comp.binaries.cbm
  2. X-Message-No: 31 (database)
  3. From: c.b.c Monthly Posting <cbm@gac.edu>
  4. Subject: BCODE/UNBCODE (Part 0/2) .c UNIX
  5. Date: Wed, 6 Dec 95 21:56:00
  6. Message-ID: <4a53ie$ghh@lunen.gac.edu>
  7. Reply-To: cbm-request@gac.edu (cbm-request)
  8. Path: mn5.swip.net!seunet!news2.swip.net!mn6.swip.net!newsfeed.sunet.se!news01.sunet.se!sunic!news99.sunet.se!newsfeed.tip.net!news.seinf.abb.se!nooft.abb.no!Norway.EU.net!EU.net!howland.reston.ans.net!newsfeed.internetmci.com!mr.net!news.mr.net!lunen.gac.edu!news
  9. Newsgroups: comp.binaries.cbm
  10. Organization: Gustavus Adolphus College
  11. Approved: mmiller3@gac.edu (comp.binaries.cbm)
  12. NNTP-Posting-Host: mmiller3@zariski.gac.edu
  13.  
  14. BCODE/UNBCODE DOCUMENTATION  for Version 2.00   [December 5, 1994]
  15.  
  16. 1. INTRODUCTION
  17.  
  18. This is the documentation for the bcode and unbcode C programs.  These
  19. programs allow you to encode binary data into a text format that can be
  20. e-mailed or posted to USENET newsgroups.  Functionally, they are quite similar
  21. to the uuencode/uudecode standard Unix utilities, except that these programs
  22. can use three different encoding formats (including "uucode") and allow for
  23. the automatic splitting, reordering, and incremental reassembly of multiple
  24. file segments for long files, and provide CRC-32 and size error checking on
  25. each encoded segment.  In other words, these are "better-mousetrap" versions
  26. of the uucode utilities.
  27.  
  28. Other Unix utilities give you some combination of these features for uuencoded
  29. data, but, as far as I am personally able to determine, all other approaches
  30. (including the "e" feature of "rn") are fundamentally flawed.  Other
  31. approaches attempt to parse the natural language of the subject lines and the
  32. text file contents to figure out the file/segment information, but this is an
  33. AI-complete problem and the results are hit-and-miss.  The approach used here
  34. alters the standard uucode format a little to give filename and segment number
  35. data in a simple and consistent manner.
  36.  
  37. There is a catch, however.  You can only extract multi-segment data that was
  38. encoded using the encoding formats defined herein.  Until the people on the
  39. other end use the encoding formats these programs give (dare to dream), the
  40. decoding program here is fundamentally no more powerful than the standard Unix
  41. uudecode program (though better-done, IMHO).
  42.  
  43. The C code is in ANSI-C format.  Both the "bcode.c" and "unbcode.c" source
  44. files contain complete programs, so there is no need for a Makefile.  Just
  45. compile and run.  The programs make no major system-specific assumptions, with
  46. the exception that the decoder uses the system calls "rename" and "remove" for
  47. files.  On some systems, these will be "link" and "unlink".  I also found that
  48. the C++ compiler I tried wanted to have "<sysent.h>" included in order to use
  49. its "link" and "unlink" calls.
  50.  
  51. 2. ENCODING/DECODING FORMATS
  52.  
  53. 2.1. NUCODE and UUCODE
  54.  
  55. The bcode and unbcode programs support four encoding formats: NUCODE, UUCODE,
  56. BCODE, and HEXCODE.  The NUCODE format is both upward and downward compatible
  57. with the old, problematic, and incomplete UUCODE format that is very popular
  58. out here in cyberspace.  Here is what the NUCODE format looks like:
  59.  
  60. -nucode-begin 2 quote2
  61. >+3$I)W,@:6YT;R!I="XB("T@0W)A:6<@0G)U8V4*
  62. `
  63. end
  64. -nucode-end 2 30 efb8f0c5
  65.  
  66. dum de dum... random separation between the segments...
  67.  
  68. -nucode-begin 1 quote2
  69. begin 640 quote2
  70. M(DEF+"!A9G1E<B!E>'1E;G-I=F4@='=E86MI;F<L('EO=7(@<')O9W)A;2!I
  71. M<R!S=&EL;"!T;V\@<VQO=RP@=')Y(&1R;W!P:6YG(&$*(&9E=R`G<VQE97`H
  72. -nucode-continued 1 90 fdcdb3d3
  73.  
  74. Here, the encoded file "quote2" is encoded into two different segments and
  75. the second segment is given first.  Try running this through the decoder
  76. and the encoded file will be extracted correctly.
  77.  
  78. The first line of segment #1 of the data begins with "-nucode-begin", which is
  79. much less likely to appear in regular text than the uucode control token
  80. "begin".  The token is followed by the segment number and filename.  For
  81. downward compatibility, the standard 'begin mode filename' line is also given
  82. so that a standard uudecode utility can decode NUCODEd data (if the data is
  83. encoded into only a single segment, which it is not in the example).  The body
  84. of the encoded data is identical to the UUCODE format, and includes the "`"
  85. and "end" lines in the final segment.  The final line of the final segment of
  86. NUCODEd data has the "-nucode-end" token, the segment number, the segment size
  87. in bytes, and a CRC-32 error checking value in hexadecimal.  The CRC algorithm
  88. used here is the same as the one used by PKZIP and ZMODEM, and a table-driven
  89. implementation is used, so calculating the CRC is no more expensive than
  90. computing a simple checksum.
  91.  
  92. If a given segment is not the final segment of a file, then the control token
  93. on the last line will be "-nucode-continued", indicating that the decoding
  94. program should be on the lookout for more segments belonging to the file.
  95. This method of encoding allows all files to be encoded in a single pass.  Oh,
  96. and of course, you can have multiple discontiguous file segments encoded in
  97. different formats in the same input file or stream with the decoder.
  98.  
  99. In previous versions of this utility, all of the control tokens had a
  100. two-hyphen prefix, which, IMHO, looks better than one, but it was pointed out
  101. that using two hyphens at the start of a line can be misinterpreted by
  102. anonymous mailing and posting services as being the beginning of a signature
  103. block, and they will erase the line starting with the two hyphens and every
  104. line following it to help retain the anonymity of the sender.  This is fine
  105. and dandy, but stripping the body of a bcoded message would not be a good
  106. thing, so the new standard for bcoded data is for all control tokens to have
  107. one hyphen, and the use of two hyphens has been relegated to the status of
  108. being a "hysterical raisin".  However, the decoder will, of course, still
  109. accept control tokens with two hyphens and the encoder will generate output
  110. with two-hyphen control tokens if you ask it to (with the -2 option).  This
  111. applies to all encoding formats supported.
  112.  
  113. 2.2. BCODE
  114.  
  115. The BCODE format, for which these utilities were originally written and named,
  116. looks like the following:
  117.  
  118. -bcode-begin 1 quote1
  119. IklmLCBhZnRlciBleHRlbnNpdmUgdHdlYWtpbmcsIHlvdXIgcHJvZ3JhbSBpcyBzdGlsbCB0
  120. b28gc2xvdywgdHJ5IGRyb3BwaW5nIGEKIGZldyAnc2xlZXAoLTEpJ3MgaW50byBpdC4iIC0g
  121. Q3JhaWcgQnJ1Y2UK
  122. -bcode-end 1 120 44fefcc6
  123.  
  124. The control lines are basically the same as for the NUCODE format (sans ugly
  125. backward-compatibility garbage) and the body is identical to the format that
  126. the BASE-64 encoding format used with MIME produces.  The difference between
  127. BCODE and MIME is that BCODE uses much simpler control information.  The two
  128. advantages of this format over NUCODE are that (1) the encoding is slightly
  129. more efficient in that you don't need the data-length character at the start
  130. of every line and the standard line length is a little longer, and (2) that
  131. no ASCII characters are used that can be easily misinterpreted or mangled in
  132. conversions to other character coding schemes.
  133.  
  134. 2.3. HEXCODE
  135.  
  136. The HEXCODE format is a very simple hexadecimal format that can be used to
  137. visually inspect a file, for downloading over a particularly unreliable
  138. connection, or for bootstrapping purposes.  HEXCODE looks like the following:
  139.  
  140. -hexcode-begin 1 quote3
  141. 000000:2249662c20616674657220657874656e7369766520747765616b696e672c2079:69
  142. 000020:6f75722070726f6772616d206973207374696c6c20746f6f20736c6f772c2074:c9
  143. 000040:72792064726f7070696e6720610a206665772027736c656570282d3129277320:24
  144. 000060:696e746f2069742e22202d2043726169672042727563650a:75
  145. -hexcode-end 1 120 44fefcc6
  146.  
  147. Each line includes the hexadecimal file position and a simple 8-bit add-up
  148. checksum.  A simple decoder program can easily be written for bootstrapping
  149. yourself if you are unable to use the C-language UNBCODE program on the
  150. target platform.
  151.  
  152. 3. BCODE PROGRAM
  153.  
  154. The usage of the BCODE program is as follows:
  155.  
  156. bcode [-vbuh12] [-l max_lines] [-p pref] [[[filename][-a encoding_alias]] ...]
  157.  
  158. The "-v" flag activates "verbose" mode, in which the program reports when it
  159. opens a file for input or output.
  160.  
  161. The "-b", "-u", and "-h" flags specify that you wish to encode in bcode,
  162. nucode, or hexcode, respectively.  The default if none of these flags are used
  163. is defined in the source code by the "DEFAULT_FORMAT" label.  The factory-set
  164. default for this label is UUCODE.  The other possible values are BCODE and
  165. HEXCODE.
  166.  
  167. The "-1" and "-2" flags tell the encoder whether to produce control tokens
  168. with one or two hyphens.  The default is the new standard, which is for one
  169. hyphen to be produced, but you can change the default by setting the
  170. "DEFAULT_TOKHYPH" label to either 1 or 2.  The decoder can, of course, handle
  171. either option.  The reasoning behind these flags was described in a previous
  172. section.
  173.  
  174. The "-l" flag and value allow you to specify the maximum number of encoded
  175. lines to include in each segment of the encoded data.  When this flag is used,
  176. output is sent to special output files rather than to stdout (where it is
  177. usually sent).  One segment is sent to each special output file.  These
  178. special output files are named after the file being encoded, appended with a
  179. ".u" followed by the at-least-two-digit segment number, for the nucode format.
  180. For example,
  181.  
  182. bcode -l 1000 junkfile
  183.  
  184. would put the bcoded segment data into "junkfile.u01", "junkfile.u02", ...,
  185. "junkfile.u99", "junkfile.u100", etc.  Each line of nucoded data contains 63
  186. characters (which represent 45 raw data bytes), so 1000 lines will produce
  187. 63000 bytes of output (counting a CR and LF at the end of each line), which is
  188. a good size for posting or for mailing to brain-damaged mailers (under 64K),
  189. with a little extra text at the top.  The max_lines value does not include the
  190. control lines in the encoding format.
  191.  
  192. For the BCODE format, the special filenames are appended with a ".b" and the
  193. segment number, and for HEXCODE, a ".h" and segment number.  If you define the
  194. "MESS_DOS" label during assembly, the special filenames will have the forms
  195. "bcNNN.bco", "uuNNN.uue", and "hexNNN.hex" for bcode, nucode, and hexcode
  196. formats, respectively.  These names are compatible with the brain-damaged
  197. MS-DOS filename format.
  198.  
  199. The "-p" flag allows you to give a filename prefix for the encoded-output
  200. files produced by the "-l" option, so that you can, for example, have the
  201. output files go into a different directory.  Since this is a prefix rather
  202. than a directory name, you have to include, on a Unix system, the extra "/"
  203. at the end of the directory name, as in "/tmp/mydir/".  The prefix argument
  204. follows the "-p" flag.
  205.  
  206. If you include filenames on the command line, then input will be taken from
  207. them in turn (otherwise, input is taken from stdin and labelled "stdin").  If
  208. there is a "-a" flag following a filename, then the file is labelled as the
  209. encoding_alias following the "-a" flag in the nucode/whatever control
  210. information.  You may include many filenames (and associated aliases) on a
  211. command line to create a nucode/whatever "archive".  You may use a "-a" flag
  212. on a command with no filenames to give your own name to the stdin stream.
  213.  
  214. 4. UNBCODE PROGRAM
  215.  
  216. The usage of the UNBCODE program is as follows:
  217.  
  218. unbcode [-ivdnf] [-p prefix] [filename ...]
  219.  
  220. The "-i", "-v", and "-d" flags are used to request different levels of
  221. operational information: informative, verbose, and debugging, respectively.
  222. Informative messages include when a file is completely pieced back together,
  223. verbose information includes when a file is opened or closed, and debugging
  224. information includes a dump of the internal "fragment" table that keeps track
  225. of which segments of which files the decoder currently has decoded.  To keep
  226. our terminology straight, a "fragment" consists of one or more file
  227. "segments".  All of this information is sent to the "stderr" file stream.
  228.  
  229. The "-n" flag is similar to the "-i" flag, except that only the filenames are
  230. spit out for the files reassembled, and the names are sent to the "stdout"
  231. file stream.  The feature is intended to make this program inter-usable with
  232. other programs.
  233.  
  234. The "-f" flag tells the decoder to forcibly accept a segment that has some
  235. kind of error in it.  Normally, when a segment is found to contain an error
  236. of any type it is simply discarded and not used.  This works well with the
  237. incremental-operation of the decoding process, which allows you to decode
  238. different segments of a single input file on multiple runs of the decoder
  239. program (i.e., you would get yourself a vaild copy of the segment in question
  240. and rerun the decoder on it later).  However, if you wish to take whatever
  241. garbled data comes out of a corrupted segment, you can give the "-f" flag.
  242. All syntax errors (e.g., bad character) in a segment are dealt with by
  243. completely ignoring the line on which the error occurred (perhaps not ideal),
  244. and error-check errors are simply ignored.  Error messages are generated but
  245. the decoded segment is kept as if it were valid.
  246.  
  247. The "-p" flag allows you to give a filename prefix to use for the temporary
  248. files that are generated during the decoding process.  Temporary files are
  249. discussed fully in a sec.  If the "-p" flag is not used, then the temporary
  250. files are maintained in the current directory, and if the "-p" flag is used,
  251. the temporary files are given whatever prefix you specify.  Normally, you
  252. would use this prefix feature to make the temporary files appear in a separate
  253. directory from the current directory (which is where the final, reassembled
  254. files will go).  Since this is a prefix, you must remember to put the final
  255. "/" on Unix directory names.  It is recommended that on a Unix system that you
  256. use something like "/tmp/mydir/" rather than just "/tmp/" to avoid name
  257. collisions with other users who might be decoding stuff at the same time.
  258.  
  259. There may not be much savings in space for keeping temp files in a separate
  260. directory, since the amount of data on hand will generally not exceed the full
  261. size of all the extracted data, because of the way that the temporary files
  262. are handled.  In other words, the usage of temporary storage is quite
  263. efficient.  There will be no savings in time for using a temp prefix, since
  264. the final files must be copied to the current directory after being extracted,
  265. whereas they are simply renamed if temps are kept in the current directory.
  266. The only real advantage is that, on some systems, the final order of extracted
  267. files in a directory will not get mish-mashed (which would only happen if the
  268. encoded data were horribly out of order, requiring the creation of lots of
  269. temporary files).
  270.  
  271. Any number of filenames may be given on the command line, and stdin is used if
  272. no names are given.  The program will do a one-pass sweep of all input files,
  273. so your system need not support random file accessing.
  274.  
  275. Intermediate segments are decoded immediately and placed into temporary files
  276. in the current directory (or prefix "directory") named like "0BC00001", with
  277. different numbers.  These files are created and deleted as needed.  Between
  278. runs, if there are any files that have not yet been completely pieced
  279. together, the "fragment" information is saved into "0BC-STAT", which can be
  280. listed to see what is in the temporary files and which segments of the files
  281. are missing.  If you use the prefix option, make sure that you use the same
  282. prefix between multiple runs if the runs are incomplete and leave this stat
  283. file lying around.  An example of this file's contents could be:
  284.  
  285. 00001-00001  beg  0000043200  0BC00002  filea
  286. 00001-00001  beg  0000003264  0BC00004  fileb
  287. 00003-00003  mid  0000000667  0BC00001  fileb
  288. 00005-00006  end  0000074586  0BC00003  fileb
  289.  
  290. The first two columns with the dash between indicate the range of segment
  291. numbers that are contained in the temporary file.  The next column gives the
  292. interpretation of the temporary file, indicating if it is the beginning,
  293. middle, or the end of the complete file being decoded.  The next column gives
  294. the number of valid bytes in the decoded fragment, and the next gives the name
  295. of the temporary file, and the final column gives the name of the file that
  296. the segment belongs to.
  297.  
  298. The fact that the status of decoding is kept between runs means that you don't
  299. have to have all of the segments of the final file(s) present at any one run.
  300. This would be useful, for example, if you were reading a binaries newsgroup
  301. that posts in a supported format and you come across a posting that has
  302. multiple file segments in multiple articles.  Rather than saving all of the
  303. pieces into a file and exiting the news reader to decode, you could use the
  304. "save to pipe" feature of most news readers.  You would enter something like:
  305.  
  306. s | unbcode -i
  307.  
  308. (or have it progammed into a copy buffer to be auto-entered at the touch of
  309. a mouse button) and the segment data would be interpreted and the decoder
  310. would inform you when it has successfully patched together a complete binary
  311. file.  This approach also works if the pieces of a posting are not only out
  312. of order, but also if multiple postings which you want are mish-mashed
  313. together.  A grander attack, again if all postings are in a supported
  314. multi-segment format, might be:
  315.  
  316. 53453-56209s | unbcode -i
  317.  
  318. If you get into trouble with a lot of garbage 0BC* files being left around
  319. after a really screwed up decode attempt, you can simply delete all of the
  320. 0BC* files to wipe the slate clean.
  321.  
  322. This program makes the following assumptions about its execution environment:
  323. we have sequential access to disk files, the append file mode is available,
  324. and the file rename and remove operations are available.  It also assumes the
  325. compiler is able to assign structures.
  326.  
  327. 5. CONCLUDING STUFF
  328.  
  329. These programs are Public Domain Software.  You may use them and distribute
  330. them freely, write filters that use the various formats, or rip into the code
  331. and extract the guts for your own purposes.  All that I ask if you modify the
  332. code is that you leave my name in to show where the code came from and add
  333. your name to take the heat off me if your modifications have bugs.
  334.  
  335. The files are also available via anonymous FTP from "ccnga.uwaterloo.ca" in
  336. directory "/pub/cbm/unix" or from the World-Wide Web in URL
  337. "http://ccnga.uwaterloo.ca/~csbruce/unix.html".
  338.  
  339. If you have any comments, questions or suggestions, you can contact me at
  340. the following e-mail address.  Oh, there is no warranty of any kind here,
  341. so if use of this program causes your company to lose millions of dollars,
  342. tough noogies.
  343.  
  344. -Craig Bruce
  345. csbruce@ccnga.uwaterloo.ca
  346. "Proposed standard unit of data storage: the 'Virtual Tree', equivalent to
  347.  120.8 megabytes."
  348.  
  349.